package com.example.opencv_demo.activity

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.ContextMenu
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.opencv_demo.R
import com.example.opencv_demo.adapter.TextAdapter
import com.example.opencv_demo.util.PhotoUtil
import java.io.File
import java.io.FileInputStream

/**
 * 图片相关demo基类
 */
abstract class ImageDemoActivity : AppCompatActivity() {
    var recyclerView: RecyclerView? = null
    var imgIndex = 0

    @JvmField
    var fileName: String? = null

    @JvmField
    var fileName2: String? = null

    @JvmField
    var imageView: ImageView? = null

    @JvmField
    var grayBitmap: Bitmap? = null

    @JvmField
    var srcBitmap: Bitmap? = null

    var mAdapter: TextAdapter? = null

    @JvmField
    var mData = ArrayList<TextAdapter.RunnableBean>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.acitvity_img_demo)
        imageView = findViewById(R.id.img)
        recyclerView = findViewById(R.id.rv_index)
        mAdapter = TextAdapter(mData, this)
        recyclerView?.setLayoutManager(LinearLayoutManager(this))
        recyclerView?.setAdapter(mAdapter)
        initData()
        mAdapter?.notifyDataSetChanged()
        imageView!!.setOnClickListener {
            imgIndex = 0
            PhotoUtil.getPhotoLib(this)
        }

        requestPower()//权限申请
    }

    abstract fun initData()

    fun showYuanTu() {
        imageView!!.setImageBitmap(srcBitmap)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_img_demo, menu);
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.getItemId()) {
            R.id.add_img -> {
                imgIndex = 1
                PhotoUtil.getPhotoLib(this)
            }
            R.id.add_img_corp -> {
                imgIndex = 1
                PhotoUtil.getPhotoLib(this, true)
            }
        }
        return true

    }

    private fun requestPower() {
//判断是否已经赋予权限
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
            != PackageManager.PERMISSION_GRANTED
        ) {
            //如果应用之前请求过此权限但用户拒绝了请求，此方法将返回 true。
            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
            ) {//这里可以写个对话框之类的项向用户解释为什么要申请权限，并在对话框的确认键后续再次申请权限.它在用户选择"不再询问"的情况下返回false
                //申请权限，字符串数组内是一个或多个要申请的权限，1是申请权限结果的返回参数，在onRequestPermissionsResult可以得知申请结果
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1
                )
            } else {
                //申请权限，字符串数组内是一个或多个要申请的权限，1是申请权限结果的返回参数，在onRequestPermissionsResult可以得知申请结果
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1
                )
            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val file = PhotoUtil.onActivityResult(requestCode, resultCode, data, this)
        file?.let {

            if (imgIndex == 0) {
                srcBitmap = BitmapFactory.decodeStream(FileInputStream(file))
                imageView?.setImageBitmap(srcBitmap)
                fileName = file.absolutePath
            } else if (imgIndex == 1) {
                findViewById<ImageView>(R.id.img2)?.setImageBitmap(
                    BitmapFactory.decodeStream(
                        FileInputStream(file)
                    )
                )
                fileName2 = file.absolutePath
            }
        }
    }
}